﻿using ETC.DLEDLightingSystemPlus.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ETC.DLEDLightingSystemPlus.Core
{
    public class JsonConfig : Bandit.UI.MVVM.NotifyObject
    {
        private static string _JsonConfigFileName = "".GetBaseDirectory() + "db.json";

        #region 实例
        private static JsonConfig _Instance;
        /// <summary>
        /// 实例
        /// </summary>
        public static JsonConfig Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new JsonConfig();
                    _Instance.InitJsonConfig();
                }
                return _Instance;
            }
            set
            {
                _Instance = value;
            }
        }
        #endregion

        private List<Tetra> _TetraItems;

        public List<Tetra> TetraItems
        {
            get { return _TetraItems; }
            set
            {
                _TetraItems = value;
                OnPropertyChanged(nameof(TetraItems));
            }
        }
        public JsonConfig()
        {

        }

        public void InitJsonConfig()
        {
            if (JsonConfig.Instance._TetraItems == null || JsonConfig.Instance._TetraItems.Count == 0)
            {
                JsonConfig.Instance._TetraItems = new List<Tetra>();
                JsonConfig.Instance._TetraItems.Add(new Tetra(Tetra.DefaultAddress, "DefaulName", "DefaulRemark"));
            }

        }
        /// <summary>
        /// Saves the config.
        /// </summary>
        /// <param name="path">Path.</param>
        public static void SaveJsonConfig(string path)
        {

            Helpers.JsonHelper.FileSerialize<JsonConfig>(JsonConfig.Instance, path);

        }

        /// <summary>
        /// Saves the config.
        /// </summary>
        public static void SaveJsonConfig()
        {
            SaveJsonConfig(JsonConfig._JsonConfigFileName);
        }

        /// <summary>
        /// Loads the config.
        /// </summary>
        /// <param name="path">Path.</param>
        public static void LoadJsonConfig(string path)
        {
            if (System.IO.File.Exists(path))
            {
                JsonConfig.Instance = Helpers.JsonHelper.FileDeserialize<JsonConfig>(path);
                JsonConfig.Instance.InitJsonConfig();
            }
            else
            {

                JsonConfig.Instance = new JsonConfig();
                JsonConfig.Instance.InitJsonConfig();
                SaveJsonConfig();
            }
        }

        /// <summary>
        /// Loads the config.
        /// </summary>
        public static void LoadJsonConfig()
        {
            LoadJsonConfig(JsonConfig._JsonConfigFileName);
        }


    }
}
